package javaBase.nowcoder.HJ43_S_迷宫问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    static int m, n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] m_n = Arrays.stream(br.readLine().split(" ")).mapToInt(
                Integer::parseInt).toArray();
        m = m_n[0];
        n = m_n[1];
        int array[][] = new int[m][n];
        for (int i = 0; i < m; i++) {
            array[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(
                    Integer::parseInt).toArray();
        }
        List<Point> points = new ArrayList<>();

        dsf(array, 0, 0, points);
        for (Point p : points) {
            System.out.println("(" + p.x + "," + p.y + ")");
        }

    }

    private static boolean dsf(int[][] array, int x, int y, List<Point> points) {
        points.add(new Point(x, y));
        array[x][y] = 1;
        if (x == m- 1 && y == n - 1) {
            return true;
        }
        if (x + 1 < m && array[x + 1][y] == 0) {
            if (dsf(array, x + 1, y, points)) {
                //System.out.println("x + 1");
                return true;
            }
        }
        if (x - 1 > -1 && array[x - 1][y] == 0) {
            if (dsf(array, x - 1, y, points)) {
                //System.out.println("x - 1");
                return true;
            }
        }
        if (y + 1 < n && array[x][y + 1] == 0) {
            if (dsf(array, x, y + 1, points)) {
                //System.out.println("y + 1");
                return true;
            }
        }
        if (y - 1 > -1 && array[x][y - 1] == 0) {
            if (dsf(array, x, y - 1, points)) {
                //System.out.println("y - 1");
                return true;
            }
        }
        points.remove(points.size() - 1);
        array[x][y] = 0;
        return false;
    }

    static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}